#include <Windows.h>
#include "CustomState.h"
#include "MainMenuState.h"
#include "GameplayState.h"
#include "../SGD Wrappers/SGD_InputManager.h"
#include "Game.h"

CustomState* CustomState::GetInstance()
{
	static CustomState s_Instance;

	return &s_Instance;
}

void	 CustomState::Enter()
{
	m_hBackground = SGD::GraphicsManager::GetInstance()->LoadTexture("resource/graphics/Large_Frame_Purple.png");

	pFont = Game::GetInstance()->GetFont();

	WIN32_FIND_DATA file;
	HANDLE hFind = INVALID_HANDLE_VALUE;
	LPWSTR Dir = new wchar_t[MAX_PATH];

	// Get directory length and then grab directory name
	DWORD DirLength = GetCurrentDirectoryA(0, 0);
	GetCurrentDirectoryW(DirLength, Dir);

	wcscat_s(Dir, MAX_PATH, L"\\resource\\custom\\*\0");

	// Get fisrt file
	hFind = FindFirstFile(Dir, &file);

	
	do
	{
		// If file is xml, then convert to xml
		if (wcsstr(file.cFileName, L".bin"))
		{
			m_szItems.push_back(file.cFileName);
		}

	} while (FindNextFile(hFind, &file) != 0); // Continue while there are still files in the directory

	m_nCursor = 0;

	m_nStart = 0;
	
	if (m_szItems.size() > 5)
		m_nEnd = 5;
	else
		m_nEnd = m_szItems.size();

	m_szHeader = L"     Custom Maps\nNumber of maps: ";
	m_szHeader.append(std::to_wstring(m_szItems.size()));

	for (unsigned int i = 0; i < m_szItems.size(); i++)
	{
		m_szItems.at(i).insert(0, std::to_wstring(i + 1) + L" ");

		int parse = m_szItems.at(i).find_last_of(L".");
		m_szItems.at(i).erase(m_szItems.at(i).begin() + parse, m_szItems.at(i).end());
	}

	delete Dir;
}
void	 CustomState::Exit()
{
	SGD::GraphicsManager::GetInstance()->UnloadTexture(m_hBackground);

	m_szItems.clear();
}
void	 CustomState::Update(float dt)
{

}
void	 CustomState::Render()
{
	SGD::GraphicsManager* pGraphics = SGD::GraphicsManager::GetInstance();
	Game* game = Game::GetInstance();

	pGraphics->DrawTexture(m_hBackground, { (game->GetScreenWidth() / 2.f) - 256, (game->GetScreenHeight() / 2.f) - 256 });
	
	pFont->Draw(&m_szHeader, (int)game->GetScreenWidth()/2 - 5*32, 100, 0.75f, SGD::Color());

	int offset = 0;

	for (unsigned int i = m_nStart; i < m_nEnd; i++)
	{
		if (m_nCursor == i)
			pFont->Draw(&m_szItems[i], int(50 * 5.5f), 200 + offset * 30, 0.50, SGD::Color(255, 255, 0));
		else
			pFont->Draw(&m_szItems[i], 50 * 5, 200 + offset * 30, 0.50, SGD::Color());
		
		offset++;
	}
}
bool	 CustomState::Input()
{
	SGD::InputManager* input = SGD::InputManager::GetInstance();
	SGD::Vector controller;

	if (m_fTimer > 0.15f)
	{
		controller = input->GetLeftJoystick(0);
		m_fTimer = 0.f;
	}

	if (input->IsKeyPressed(SGD::Key::W)
		|| input->IsKeyPressed(SGD::Key::UpArrow)
		|| controller.y < -0.2f)
	{
		--m_nCursor;

		// Stop at top
		if (m_nCursor < 0)
			m_nCursor = 0;

		if (m_nCursor < (int)m_nStart && m_nStart != 0)
		{
			m_nEnd--;
			m_nStart--;
		}
	}

	if (input->IsKeyPressed(SGD::Key::S)
		|| input->IsKeyPressed(SGD::Key::DownArrow)
		|| controller.y > 0.2f)
	{
		++m_nCursor;

		// Stop at the bottom
		if (m_nCursor >(int)m_szItems.size() - 1)
			m_nCursor = m_szItems.size() - 1;

		if (m_nCursor == m_nEnd && m_nEnd < (unsigned int)m_szItems.size())
		{
			m_nEnd++;
			m_nStart++;
		}
	}

	if (SGD::InputManager::GetInstance()->IsKeyPressed(SGD::Key::Escape)
		|| SGD::InputManager::GetInstance()->IsButtonDown(0, 1))
		Game::GetInstance()->ChangeState(MainMenuState::GetInstance());

	if ((input->IsKeyPressed(SGD::Key::Enter) || input->IsButtonPressed(0, 0)) && m_szItems.size())
	{
		m_szItems.at(m_nCursor).erase(m_szItems.at(m_nCursor).begin(), m_szItems.at(m_nCursor).begin() + 2);
		m_szItems.at(m_nCursor).insert(0, L"resource\\custom\\");
		m_szItems.at(m_nCursor).append(L".bin");

		m_szMap.resize(m_szItems.at(m_nCursor).size());

		for (unsigned int i = 0; i < m_szItems.at(m_nCursor).size(); i++)
			m_szMap[i]= (char)m_szItems.at(m_nCursor).at(i);

		

		Game::GetInstance()->ChangeState(GameplayState::GetInstance());
	}


	return true;
}